package com.usian.user.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.admin.AdminConstans;
import com.usian.model.article.pojo.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApAuthorFeign apAuthorFeign;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto ==null) return errorParam;
        return getPageData(dto,Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() !=null,ApUserRealname::getStatus,dto.getStatus()));
    }

    //认证 自媒体 作者 用户
    // @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        if (dto==null ||dto.getId()==null) return errorParam;
        //认证
        ApUserRealname attestation=getById(dto.getId());
        ApUser User_c = apUserMapper.selectById(attestation.getUserId());
        System.out.println(User_c);
        //用户
        ApUserRealname realname = new ApUserRealname();
        realname.setStatus(status);
        realname.setId(dto.getId());
        //失败原因
        if (StringUtils.isNotEmpty(dto.getMsg()))realname.setReason(dto.getMsg());
        updateById(realname);

        if (status == AdminConstans.PASS_AUTH){
            WmUser WmUser = wmUserFeign.findByName(User_c.getName());
            if (WmUser==null){
                WmUser  = new WmUser();
                WmUser.setApUserId(attestation.getUserId());
                BeanUtils.copyProperties(User_c,WmUser);
                // PropertyUtils.copyProperties();  类型不会自动转换
                WmUser.setCreatedTime(new Date());
                WmUser.setStatus(9);
                ResponseResult save = wmUserFeign.save(WmUser);
                LinkedHashMap map= (LinkedHashMap) save.getData();
                WmUser.setId((Integer) map.get("id"));

            }
            ApAuthor apAuthor = apAuthorFeign.findByUserId(User_c.getId());
            if (apAuthor==null){
                ApAuthor author = new ApAuthor();
                author.setName(User_c.getName());
                author.setUserId(User_c.getId());
                author.setCreatedTime(new Date());
                author.setWmUserId(WmUser.getId());
                author.setType(2);
                ResponseResult result = apAuthorFeign.save(author);
                LinkedHashMap map= (LinkedHashMap) result.getData();
                WmUser.setApAuthorId((Integer) map.get("id"));
                wmUserFeign.updateAuthorId(WmUser);
            }
            ApUser tempUser = new ApUser();
            tempUser.setId(User_c.getId());
            tempUser.setFlag((short)1);
            apUserMapper.updateById(tempUser);
        }
        return succes;
    }

}
